<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/8/18 0018
  Time: 20:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.12.4.js"></script>
    <link href="${pageContext.request.contextPath}/layui/css/layui.css" type="text/css" rel="stylesheet">
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>

    <script type="text/javascript">

        layui.use('table',function(){

            var table = layui.table;

            table.render({
                elem:"#myTable",//表格的id
                url:"${pageContext.request.contextPath}/login/selectByPageUser",//数据接口 请求地址
                page:true,//开启分页s
                //发送的额外参数
                where:{"name":"${param.name}"},
                //顶部工具栏
                toolbar:"#mytoolbar",
                //页面初始大小
                limit:2,
                //页面下拉列表框中的内容
                limits:[1,2,3,4,5,10,20,30,50],
                //列
                cols:[[
                    {type:"checkbox"},
                    {title:"编号",field:"userId"},
                    {title:"用户名",field:"userName"},
                    {title:"密码",field:"password"},
                    {title:"操作",templet:"#caoZuo"}

                ]]
            });
        });

        //搜索操作
        function doSearch () {

            //获取用户输入的内容
            var value = $("#gname").val();

            //发送请求 并且接收数据
            layui.use('table',function(){

                var table = layui.table;
                //reload方法完成数据的重新加载
                table.reload('myTable',{
                    //发送的额外参数
                    where:{"name":value}
                });
            });


        }

        layui.use(['form','layer','table'],function(){
            var form =  layui.form;
            var layer = layui.layer;
            var table = layui.table;

            form.verify({
                //dName:function(value,item) {//value：输入框里面的值 item：DOM对象


              //  }

            });


            //当点击提交按钮的时候，会进入到这个函数


            table.on('tool(test)',function(obj){

                // var data = obj.data();
                // var  iayEvent = obj.event;
                // var tr = obj.tr;




                // console.log(iayEvent);
                // console.log(tr);
            });
            //当所有的验证通过  会进入到此函数
            form.on("submit(add)",function (data) {

                    $.ajax({
                        url:"${pageContext.request.contextPath}/login/insertUser",
                        data:data.field,
                        success:function (data) {

                            if(data.isInsert){
                                layer.alert("添加成功");
                            }else{
                                layer.alert("添加失败");
                            }
                        }
                    });
            });
            form.on("submit(update)",function (data) {
        alert("1111");
                $.ajax({
                    url:"${pageContext.request.contextPath}/login/updateUser",
                    data:data.field,
                    success:function (data) {
                        if(data.isUpdate){
                            alert("修改成功");
                        }else {
                            alert("修改失败");
                        }

                        //上传成功 关闭弹出层
                        layer.closeAll("page");
                        //刷新table 刷新table
                        table.reload("myTable");
                    }
                });
            })


        })






        //让添加的弹出层显示  打开
        function toOpenLayer() {

            layui.use('layer',function(){
               layer.open({
                   title:"添加上师",
                   content:$("#addForm"),
                   type:1,
                   maxmin:true
               })
            });
        }

        function doDeleteQuantity() {
            alert("11111")
            layui.use(['table','layer'],function(){

                var table  = layui.table;
                //获取选中的复选框
                var checkStatu = table.checkStatus("myTable");
                //获取选中行的数据
                var data = checkStatu.data;
                console.log(data);

                if(data.length==0){
                    layer.alert("请选中要删除的内容");
                }else{

                    layer.confirm("确定删除吗？",function(index){

                        var ids =new Array(data.length);

                        for(var i = 0;i<ids.length;i++){
                            ids[i]=data[i].patientId;
                        }


                        console.log(ids);
                        $.ajax({
                            url:"private/deletequantity",
                            data:"ids="+ids,
                            success:function(data){

                                layer.alert(data.isDeleteBatch,{time:2000});
                                table.reload("myTable");

                            }
                        });




                    });


                }

            });
        }

        function deleteDt(id) {
            alert(id);
            layui.use(['table','layer'],function () {

                var table=layui.table;
                $.ajax({
                    url:"${pageContext.request.contextPath}/login/deleteUser",
                    data:{"id":id},
                    success:function(data){

                        console.log(data);
                        if(data.isDelete){
                            layer.alert("删除成功",{time:2000});
                            //刷新table
                            table.reload("myTable");
                        }else{
                            layer.alert("删除失败",{time:2000});
                        }

                    }
                });

            })


        }

        function doUpdateLayer(id) {
        alert(id);
            $.ajax({
                url:"${pageContext.request.contextPath}/login/selectOneUser",
                data:{"id":id},
                success:function(data){
                    console.log(data);

                    //打开显示  修改的弹出层
                    layer.open({
                        title:"修改用户",
                        content:$("#updateForm"),
                        type:1,
                        maxmin:true
                    });


                    $("#userid").val(data.userId);
                    $("#uusername").val(data.userName);
                    $("#upassword").val(data.password);


                    //渲染
                    layui.form.render();


                }
            });

        }





    </script>
    <script type="text/html" id="image">

        <%--{{#  if(d.patientSex==0){ }}--%>
        <%--女--%>
        <%--{{#  } else { }}--%>
        <%--男--%>
        <%--{{#  } }}--%>
        <img src="${pageContext.request.contextPath}/img/{{d.guruImage}}">
</script>

    <script type="text/html" id="bannerState">

        {{# if (d.guruStatus=== 0) { }}
        正常
        {{# } else { }}
        冻结
        {{# } }}

    </script>

    <script type="text/html" id="caoZuo">
        <button type="button" class="layui-btn layui-btn-radius layui-btn-normal" onclick="doUpdateLayer({{d.userId}})" >修改</button>
        <button type="button" class="layui-btn layui-btn-danger" onclick="deleteDt({{d.userId}})" >删除</button>
    </script>

    <style type="text/css">
        .layui-table-cell{
            height: auto;
            line-height: 50px;
        }

    </style>
   
</head>
<body>

<table id="myTable" lay-data="{id:'myTable'}" >

</table>
dsadsad


<div id="mytoolbar"  style="display: none">
    <button  type="button" class="layui-btn " onclick="toOpenLayer()">

        <i class="layui-icon">&#xe608;</i> 添加
    </button>
    <button type="button" class="layui-btn layui-btn-radius layui-icon-danger" onclick="doDeleteQuantity()">
        <i class="layui-icon layui-icon-delete " ></i> 批量删除
    </button>


    <div class="layui-inline">
        <label class="layui-form">名字</label>
        <div class="layui-input-inline" style="width: 200px">
            <input type="text"  placeholder="请输入上师名字" class="layui-input" id="gname">
        </div>
    </div>
    <div class="layui-input-inline" style="width:100px">
        <button type="button" class="layui-btn layui-btn-normal" onclick="doSearch()">
            <i class="layui-icon layui-icon-search"></i> 搜索
        </button>
    </div>
</div>


<form id="addForm" style="display: none" class="layui-form" >
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input  type="text" id="userName" name="userName" class="layui-input"  />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input name="password"  id="password" class="layui-input" placeholder="请输入密码" />
        </div>

    </div>

    <div class="layui-form-item">
        <button lay-submit type="submit" lay-filter="add" id="addgSubmit" class="layui-btn">添加用户</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>


<form id="updateForm" style="display: none" class="layui-form">

    <div class="layui-form-item">
        <label class="layui-form-label">用户姓名</label>
        <div class="layui-input-block">
            <input name="userName" id="uusername" class="layui-input" />
            <input name="userId" id="userid" type="hidden" class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户密码</label>
        <div class="layui-input-block">
            <input name="password"  id="upassword" class="layui-input"  />
        </div>

    </div>


    <div class="layui-form-item">
        <button lay-submit type="button" id="updateForm2" lay-filter="update" class="layui-btn">修改用户</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

</body>
</html>
